/**
 * 
 */
package whf.framework.tools.data.export;

import java.io.OutputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import whf.framework.entity.Entity;
import whf.framework.meta.MetaFactory;
import whf.framework.service.Service;
import whf.framework.util.BeanFactory;

/**
 * @author wanghaifeng
 * @create Oct 16, 2006 11:29:05 AM
 * 
 */
@SuppressWarnings("unchecked")
public class Export2Xml implements Export {

	/* (non-Javadoc)
	 * @see whf.framework.tools.data.export.Export#export(java.io.OutputStream, java.lang.Class, java.lang.String, int)
	 */
	public void export(OutputStream out, Class clsName, String queryString) throws ExportException{
		if(out == null) throw new ExportException("Target output stream is null!");
		if(clsName == null) throw new ExportException("Businese object class name is null!");
		try{
			Service service = BeanFactory.getService(MetaFactory.findByModelClass(clsName));
			List<Entity> list = service.findWithoutLazy(queryString);
			Element root = new Element("root");
			for(Entity entity: list){
				root.addContent(entity.toXmlElement());
			}
			Document doc = new Document(root);
			XMLOutputter outputter = new XMLOutputter();
			outputter.output(doc, out);
		}catch(Exception e){
			throw new ExportException("Error occurs", e);
		}
	}
}
